import 'package:dio/dio.dart';
import 'package:flutter_simple_watch/news/api/we_chat_api_service.dart';

///@author aidenquan
/// Created by junlintianxia on 1/15/21.

/// 微信消息通用参数拦截器
class WeChatParamInterceptor extends InterceptorsWrapper {
  static const String apiKey = '0b2bdeda43b5688921839c8ecb20399b';

  WeChatParamInterceptor();

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    var method = options.method;
    if (method == 'GET') {
      options.queryParameters = _getAddParams(options);
    } else {
      options.data = _postAddParams(options);
    }
    // 继续请求责任链
    super.onRequest(options, handler);
  }

  Map<String, dynamic> _getAddParams(RequestOptions options) {
    var params = options.queryParameters;
    var commonParams = getCommonParams();
    for (String key in params.keys) {
      commonParams[key] = params[key];
    }
    return commonParams;
  }

  Map<String, dynamic> _postAddParams(RequestOptions options) {
    var newData = RequestOptions().data;
    var data = options.data;
    var commonParams = getCommonParams();

    /// 添加通用元素
    for (String key in commonParams.keys) {
      newData[key] = commonParams[key];
    }

    /// 参数回填
    for (String key in data.keys) {
      newData[key] = data[key];
    }
    return newData;
  }

  /// 添加并获取通用参数 子类可以复写
  Map<String, dynamic> getCommonParams() {
    Map<String, dynamic> commonParams = {};
    commonParams['key'] = WeChatApiService.WECHAT_APP_KEY;
    return commonParams;
  }
}